<?php
/***********************************************************************
 * @package     IvanEngine
 * @subpackage  Framwork
 * @author      Ivan <ivanzx@msn.com>
 * @created     2010-07-15
 ***********************************************************************/
if(defined("IVANENGINE_FRAMEWORK_THEMESMANAGER"))
    die("Redefinition macro in :".__FILE__."(".__LINE__.")");
define("IVANENGINE_FRAMEWORK_THEMESMANAGER", true);

require_once (dirname(__FILE__).'/Common.php');

class CThemesManager extends CSingleton
{
    static protected $m_this    = null;
    protected
        $m_themesList           = null,
        $m_curTheme            = 'default',
        $m_themesPath           = null,
        $m_themesUrl            = null;
    public function __construct()
    {
        parent::__construct();
        $this->m_themesPath     = CFileHelper::CorrectPath(
            CConfig::Option(CConfig::OT_ThemesPath));
        $this->m_themesList     = new CVector();
        $this->m_curTheme      = CConfig::Option(CConfig::OT_CurrentThemes);
    }
    
    public function __destruct()
    {
        unset($this->m_themesList);
        parent::__destruct();
    }
    
    static public function Get()
    {
        if( self::$m_this == null )
        {
            parent::StartSingleton();
            self::$m_this = new CThemesManager();
            parent::EndSingleton();
        }
        return self::$m_this;
    }
    
    public function Initialize()
    {
        if( $this->m_themesPath == null ){
            throw new CExceptionInvalidArgument('<b>CThemesManager initialize failed:<b><br>'.
                'You must set themes path in config file.');
        }
        $themeses = CFinder::Create(CFinder::FT_Dir)->MaxDepth(1)->Go($this->m_themesPath);
        $tempStr  = new CString();
        foreach( $themeses as $themes )
        {
            $tempStr->Set($themes);
            $tempStr->TrimRight(M_DS);
            $pos = $tempStr->FindLastOf(M_DS);
            if( $pos != CString::npos ){
                $themesName = $tempStr->SubStr($pos+1);
            }
            $this->m_themesList->Add($themesName);
        }
        unset($tempStr);
    }
    
    public function SwitchThemes($themesName)
    {
        if( !$this->m_themesList->Get($themesName) ){
            throw new CExceptionRuntime('Can not find the themes: '.$themesName);
        }
        $this->m_curTheme  = $themesName;
        $themesContent = 'SwitchThemes(\''.$this->m_themesPath.M_DS.
            $this->m_curTheme.M_DS.'default.css\');';
        $themesContent = CTagHelper::ContentTag('script', $themesContent);
        CPrinter::Get()->GetRoot()->AddContent($themesContent);
    }
    
    public function GetCurTheme()
    {
        return $this->m_curTheme;
    }
    
    public function & GetAllThemes()
    {
        return $this->m_themesList;
    }
    
    public function GetThemesPath()
    {
        return $this->m_themesPath;
    }
    
    public function GetThemePath($themeName)
    {
        return $this->m_themesPath.$themeName.M_DS."default.css";
    }
    
    public function GetCurThemePath()
    {
        return $this->GetThemePath($this->m_curTheme);
    }
    
    public function GetThemesUrl()
    {
        if( empty($this->m_themesUrl) )
        {
            $pia    = CRequest::Get()->GetPathInfoArray();
            $url    = 'http://'.$pia['HTTP_HOST'].CRequest::Get()->GetRelativeUrlRoot().'/';
            $subUrl = CUrlHelper::CorrectUrl(CFileHelper::AToR($this->m_themesPath));
            if( $subUrl[0] == '/' ){
                $subUrl= substr($subUrl, 1);
            }
			if( $subUrl[strlen($subUrl)-1] != '/' ){
                $subUrl= $subUrl.'/';
            }
            $this->m_themesUrl = $url.$subUrl;
        }
        
        return $this->m_themesUrl;
    }
    
    public function GetThemeUrl($themeName)
    {
        return $this->GetThemesUrl().$themeName.'/default.css';
    }
    
    public function GetCurThemeUrl()
    {
        return $this->GetThemesUrl().$this->m_curTheme.'/default.css';
    }
}

?>
